<?php

/**
 * Główny plik rozruchowy DSF Framework
 * 
 */

date_default_timezone_set('Europe/Warsaw');
define('DSF_APPATH', __DIR__);
define('DS', DIRECTORY_SEPARATOR);

session_start();

require_once 'application.cfg'; // import konifguracji strony

function DsfDefineConstans($domain, $port, $path){
    define('DSF_URLAPPPATH', $path);
    define('DSF_URLAPPHOST', $domain);
    define('DSF_URLAPPPORT', $port);
}

function RunApplication($domain = 'localhost', $path = '/', $port = '80') {
    
    DsfDefineConstans($domain, $port, $path);

    require_once DSF_APPATH . DS . 'sys' . DS . 'Core.Consts.php';
    require_once DSF_SYSDIR . DS . 'Core.Kernel.php';

    define('DSF_URLDEFCONT', 'default');
    define('DSF_URLDEFACT', 'index');

    loadCoreLibs([
        'Core.Url',
        'Core.View',
    ]);

    if (file_exists(DSF_APPATH . DS . 'service.mode.on')) {
        echo View::factory('servicePage');
        exit;
    } else {
        $_UrlPath = (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') ? $_SERVER['PATH_INFO'] : '/' . DSF_URLDEFCONT . '/' . DSF_URLDEFACT;
        $_pathArray = explode('/', $_UrlPath);

        $_UrlPathArr = [];

        for ($i = 0; $i < count($_pathArray) - 1; $i++) {
            $_UrlPathArr[$i] = $_pathArray[$i + 1];
        }

        define('DSF_CURRENT_CONTROLER', $_UrlPathArr[0]);
        if (isset($_UrlPathArr[1]))
            define('DSF_CURRENT_ACTION', $_UrlPathArr[1]);
        else
            define('DSF_CURRENT_ACTION', DSF_URLDEFACT);

        return $_UrlPathArr;
    }
}